package soulkun.library.helper.sharedpreferences

import android.content.Context
import android.content.SharedPreferences
import soulkun.library.LibraryInit

class SharedPreferencesHelper(private var name: String, mode: Mode = Mode.MODE_PRIVATE) {
    
    enum class Mode{
        MODE_PRIVATE, 
        MODE_WORLD_READABLE, 
        MODE_WORLD_WRITEABLE, 
        MODE_MULTI_PROCESS,
    }

    private var sharedPreferences: SharedPreferences = when (mode) {
        Mode.MODE_PRIVATE -> LibraryInit.context.getSharedPreferences(
            name,
            Context.MODE_PRIVATE
        )
        Mode.MODE_WORLD_READABLE -> LibraryInit.context.getSharedPreferences(
            name,
            Context.MODE_WORLD_READABLE
        )
        Mode.MODE_WORLD_WRITEABLE -> LibraryInit.context.getSharedPreferences(
            name,
            Context.MODE_WORLD_WRITEABLE
        )
        Mode.MODE_MULTI_PROCESS -> LibraryInit.context.getSharedPreferences(
            name,
            Context.MODE_MULTI_PROCESS
        )
    }

    fun getInt(key: String): Int {
        return getInt(key, 0)
    }

    fun getInt(key: String, defValue: Int): Int {
        return sharedPreferences.getInt(key, defValue)
    }

    fun putInt(key: String, value: Int) {
        val edit = sharedPreferences.edit()
        edit.putInt(key, value)
        edit.apply()
    }

    fun getLong(key: String): Long {
        return getLong(key, 0)
    }

    fun getLong(key: String, defValue: Long): Long {
       return sharedPreferences.getLong(key, defValue)
    }

    fun putLong(key: String, value: Long) {
        val edit = sharedPreferences.edit()
        edit.putLong(key, value)
        edit.apply()
    }

    fun getFloat(key: String): Float {
        return getFloat(key, 0.0f)
    }

    fun getFloat(key: String, defValue: Float): Float {
        return sharedPreferences.getFloat(key, defValue)
    }

    fun putFloat(key: String, value: Float) {
        val edit = sharedPreferences.edit()
        edit.putFloat(key, value)
        edit.apply()
    }

    fun getBoolean(key: String): Boolean {
        return getBoolean(key, false)
    }

    fun getBoolean(key: String, defValue: Boolean): Boolean {
        return sharedPreferences.getBoolean(key, defValue)
    }

    fun putBoolean(key: String, value: Boolean) {
        val edit = sharedPreferences.edit()
        edit.putBoolean(key, value)
        edit.apply()
    }

    fun getString(key: String): String {
        return getString(key, "")
    }

    fun getString(key: String, defValue: String): String {
        return sharedPreferences.getString(key, defValue) ?: defValue
    }

    fun putString(key: String, value: String) {
        val edit = sharedPreferences.edit()
        edit.putString(key, value)
        edit.apply()
    }

    fun setName(name: String, mode: Mode = Mode.MODE_PRIVATE) {
        this.name = name
        sharedPreferences = when (mode) {
            Mode.MODE_PRIVATE -> LibraryInit.context.getSharedPreferences(
                name,
                Context.MODE_PRIVATE
            )
            Mode.MODE_WORLD_READABLE -> LibraryInit.context.getSharedPreferences(
                name,
                Context.MODE_WORLD_READABLE
            )
            Mode.MODE_WORLD_WRITEABLE -> LibraryInit.context.getSharedPreferences(
                name,
                Context.MODE_WORLD_WRITEABLE
            )
            Mode.MODE_MULTI_PROCESS -> LibraryInit.context.getSharedPreferences(
                name,
                Context.MODE_MULTI_PROCESS
            )
        }
    }
    
}